﻿package  
{
	import com.hurlant.crypto.symmetric.BlowFishKey;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	/**
	 * @author s
	 */
	public class InitJobCounter extends EventDispatcher
	{
		private static var _instance:InitJobCounter = null;
		private var _counter:int;
		private var _remainTasks:Array;
		
		// events
		public static const COMPLETE:String = "JOB_COMPLETE";
		public static const UP:String = "JOB_UP";
		public static const DOWN:String = "JOB_DOWN";

		public function InitJobCounter(a:Blocker) 
		{
			_counter = 0;
			_remainTasks = new Array;
		}
		
		public static function getInstance():InitJobCounter
		{
			if (_instance == null) {
				_instance = new InitJobCounter(new Blocker);
			}
			return _instance;
		}
		
		public function up(taskName:String):void 
		{
			_counter++;
			_remainTasks.push(taskName);
			dispatchEvent(new Event(UP));
		}
		
		public function down(taskName:String):void 
		{
			if (_counter > 0) {
				var taskIndex:int = _remainTasks.indexOf(taskName);
				if (taskIndex >= 0) {
					_remainTasks.splice(taskIndex, 1);
				}
				_counter--;
				dispatchEvent(new Event(DOWN));
				if(_counter == 0){
					dispatchEvent(new Event(COMPLETE));
				}
			}
		}
		
		public function get count():int 
		{
			return _counter;
		}
		
		public function get remainTasks():Array 
		{
			return _remainTasks;
		}
	}
}

internal class Blocker{}